package com.hdw.common.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by liujunlei on 2021/7/16.
 */
public class LocalDateUtil {

    public static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd HH:mm:ss");
    public static SimpleDateFormat dayFormat = new SimpleDateFormat("yyyyMMdd");
    public static SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");

    public static Date dateCalculate(Date oriDate,int type,int diff) {
        Calendar date = Calendar.getInstance();
        date.setTime(oriDate);
        date.add(type,diff);
        return new Date(date.getTimeInMillis());
    }

    public static Date nextDayBegin(Date date) {
        Date nextDate = dateCalculate(date,Calendar.DAY_OF_MONTH,1);
        String str = new SimpleDateFormat("yyyy-MM-dd").format(nextDate);
        try {
            date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(str+" 00:00:00");
            return date;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 字符串时间差，>60换算成分钟，>3600换算成小时
     * @return 8小时13分钟20秒，13分钟20秒等
     */
    public static String dateDiffString(Date begin,Date end) {
        Long diff = (end.getTime()-begin.getTime())/1000; //换算成秒
        Long hour = diff/3600;
        Long min = (diff%3600)/60;
        Long secord = ((diff%3600)%60);
        return (hour>0?hour+"小时":"")+(min>0?min+"分":"")+(secord>0?secord+"秒":"");
    }
}
